package com.example.zhuyu.basemvp.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

/**
 * @author zhuyu
 * @date 2019/2/18
 */
public abstract class BaseFragment<P extends IPresenter> extends RxFragment implements IFragment {

    protected P mPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = null;

        try {
            int layout = initLayout(savedInstanceState);

            if (layout != 0) {

                view = inflater.inflate(layout, container, false);

            }
        } catch (Exception e) {

            e.printStackTrace();

        }

        return view;

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init(savedInstanceState);
        mPresenter = createPresenter();
        onRequest(this);
    }

    /**
     * 提供给 V 层 创建 Presenter
     *
     * @return Presenter
     */
    protected abstract P createPresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }
}
